package com.ajk.server.bkcontroller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ajk.server.exception.AjkException;
import com.ajk.server.exception.ErrorConstant;
import com.ajk.server.model.Knowledge;
import com.ajk.server.model.query.KnowledgeQuery;
import com.ajk.server.model.request.KnowledgeReq;
import com.ajk.server.model.response.KnowledgeRes;
import com.ajk.server.model.response.Result;
import com.ajk.server.model.response.ResultList;
import com.ajk.server.model.response.ResultObject;
import com.ajk.server.service.KnowledgeService;
import com.ajk.server.utils.FileStoreUtil;

/**
 * 疾病相关接口
 * @author chenxuan
 * 
 * t_disease
 * dict_bodypart
 * r_disease_bodypart
 */
@Controller
@RequestMapping(value="/admin/knowledge")
public class AdminKnowledgeController {
	
	Logger logger = LoggerFactory.getLogger(AdminKnowledgeController.class);

	@Autowired
	private KnowledgeService knowledgeService;
	
	/**
	 * 添加健康资讯
	 * @param knowledgeReq
	 * @return
	 */
	@RequestMapping(value="/add",  method = RequestMethod.POST, consumes = "multipart/form-data")
	@ResponseBody
	public ResultObject<Integer> add(KnowledgeReq knowledgeReq){
		Integer id = knowledgeService.add(knowledgeReq);
		return new ResultObject<Integer>(id);
	}
	
	/**
	 * 疾病分类列表
	 * @return
	 */
	@RequestMapping(value="/delete/{id}")
	@ResponseBody
	public Result delete(@PathVariable("id") Integer id){
		knowledgeService.delete(id);
		return new Result();
	}
	
	@RequestMapping(value="/update", method = RequestMethod.POST, consumes = "multipart/form-data")
	@ResponseBody
	public Result update(KnowledgeReq knowledge){
		if(knowledge.getId()==null || knowledge.getId()<=0){
			throw new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "ID不能为空");
		}
		knowledgeService.update(knowledge);
		return new Result();
	}
	
	/**
	 * 文章列表
	 * @param knowledge
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public ResultObject<KnowledgeRes> detail(@PathVariable("id") Integer id){
		KnowledgeRes  knowledgeRes = knowledgeService.getById(id);
		return new ResultObject<>(knowledgeRes);
	}
	
	@ResponseBody
	@RequestMapping(value = "/query", method = RequestMethod.GET)
	public ResultList<Knowledge> query(KnowledgeQuery query){
		ResultList<Knowledge> resultList = new ResultList<>(); 
		
		List<Knowledge> knowledges = knowledgeService.findKnowledgeByQuery(query);
		resultList.setItemList(knowledges);
		
		Integer count = knowledgeService.countKnowledgeByQuery(query);
		query.setCount(count);
		
		resultList.setExt(query);
		
		return resultList;
	}
}